using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace GameFrameWork
{
    public class UICutImage : Image
    {
        public OriginHorizontal cutOrigin = OriginHorizontal.Left;

        [Range(0, 1)]
        public float minHorRate = 0;
        [Range(0, 1)]
        public float maxHorRate = 1;
        [Range(0, 1)]
        public float minVerRate = 0;
        [Range(0, 1)]
        public float maxVerRate = 1;

        protected override void OnPopulateMesh(VertexHelper toFill)
        {
            base.OnPopulateMesh(toFill);

            List<UIVertex> vertexes = new List<UIVertex>();
            toFill.GetUIVertexStream(vertexes);

            float minX = rectTransform.rect.x + rectTransform.rect.width * (cutOrigin == OriginHorizontal.Left ? minHorRate : (1 - minHorRate));
            float maxX = rectTransform.rect.x + rectTransform.rect.width * (cutOrigin == OriginHorizontal.Left ? maxHorRate : (1 - maxHorRate));
            float minY = rectTransform.rect.y + rectTransform.rect.height * minVerRate;
            float maxY = rectTransform.rect.y + rectTransform.rect.height * maxVerRate;

            UIVertex vertex = new UIVertex();
            toFill.PopulateUIVertex(ref vertex, 0);
            vertex.position.Set(minX, maxY, vertex.position.z);
            vertex.uv0 = new Vector4(minHorRate, maxVerRate, vertex.uv0.z, vertex.uv0.w);
            toFill.SetUIVertex(vertex, 0);

            vertex = new UIVertex();
            toFill.PopulateUIVertex(ref vertex, 1);
            vertex.position.Set(minX, minY, vertex.position.z);
            vertex.uv0 = new Vector4(minHorRate, minVerRate, vertex.uv0.z, vertex.uv0.w);
            toFill.SetUIVertex(vertex, 1);

            vertex = new UIVertex();
            toFill.PopulateUIVertex(ref vertex, 2);
            vertex.position.Set(maxX, minY, vertex.position.z);
            vertex.uv0 = new Vector4(maxHorRate, minVerRate, vertex.uv0.z, vertex.uv0.w);
            toFill.SetUIVertex(vertex, 2);

            vertex = new UIVertex();
            toFill.PopulateUIVertex(ref vertex, 3);
            vertex.position.Set(maxX, maxY, vertex.position.z);
            vertex.uv0 = new Vector4(maxHorRate, maxVerRate, vertex.uv0.z, vertex.uv0.w);
            toFill.SetUIVertex(vertex, 3);
        }
    }
}



